﻿using System.Configuration;
using System.Net.Configuration;
using System.Web.Mail;
using System.Xml;

namespace Core.Library
{
    public class MailUtility
    {        
        public static bool Send(string to, string subject, string body)
        {
            try
            {
                var smtpSec = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");

                // Initialize the mailmessage class
                var mailMsg = new MailMessage
                {
                    From         = smtpSec.Network.UserName,
                    To           = to,
                    Subject      = subject,
                    BodyFormat   = MailFormat.Html,
                    Body         = body,
                    Priority     = MailPriority.High,
                    BodyEncoding = System.Text.Encoding.UTF8
                };

                // Smtp configuration
                SmtpMail.SmtpServer = smtpSec.Network.Host;
                mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", smtpSec.Network.UserName);
                mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", smtpSec.Network.Password);
                mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", smtpSec.Network.Port.ToString());
                mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");

                SmtpMail.Send(mailMsg);
                return true;
            }
            catch { return false; }
        }
    }
}
